package com.tao.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
  
	@SuppressWarnings("finally")
	public  String sendGet(String requestUrl,String cookie){
		 
		 String result = null;
	        HttpClient client = new DefaultHttpClient();
	        HttpGet get = new HttpGet(requestUrl);
	        get.addHeader(new BasicHeader("cookie", cookie)); 
	        try {
	        	
	            HttpResponse res = client.execute(get);
	            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
	                HttpEntity entity = res.getEntity();
	                result = EntityUtils.toString(entity,"UTF-8");
	                return  result;
	            }
	        } catch (Exception e) {
	        	
	            e.printStackTrace();
	        }finally{
	        	return result; 	
	        }
			
 }
	 @SuppressWarnings("finally")
	public   String sendPost(String requestUrl,String cookie,String stringEntity){
		 String result = null;
//	        System.out.println(stringEntity);
	        HttpClient client = new DefaultHttpClient();
	        
	        HttpPost post = new HttpPost(requestUrl);
	        
	        post.addHeader(new BasicHeader("cookie", cookie)); 
	        
	        try {
	        	StringEntity s = new StringEntity(stringEntity,"UTF-8");
	            post.setEntity(s);
	            HttpResponse res = client.execute(post);
	            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
	                HttpEntity entity = res.getEntity();
	                result = EntityUtils.toString(entity,"UTF-8");
	                return  result;
	            }
	            
	        } catch (Exception e) {
	        	
	            e.printStackTrace();
	        }finally{
	        	return result; 	
	        }
			
}
	 public static void main(String[] args) {
		 String s ="{\"Msg\":{\"ClientMsgId\":\"1477268247199\",\"Content\":\"dsfasdadas\",\"FromUserName\":\"@9dd35f8af5d4d995c4e1b03e372b5fe47d68afafaa11c6312e7d80d81acd0566\",\"LocalID\":\"1477268247199\",\"ToUserName\":\"@@2279384302d1262c7acc2bc5b21710fe9a5366df631b72697dcbbc95ae0ce841\",\"Type\":1},\"BaseRequest\":{\"DeviceID\":\"e894762829892471\",\"Sid\":\"RPdJRTCZA6HCIusE\",\"Skey\":\"@crypt_fd1a2568_7536a4962ea118b09abda8b61cf48fd7\",\"Uin\":\"2190998410\"}}";

		String d = new HttpUtil(). sendPost(UrlConf.webwxSendMsg, null, s);
		System.out.println(d);
	 }
}
